package com.hzdracom.epubreader.views;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;

import com.shwread.android.qysw10000182.R;

public class BrightControlView implements OnClickListener {
	
	private static BrightControlView instance;
	private PopupWindow popupWindow;
	
	private Context mContext;
	
	private SeekBar sbBright;
	private Button btnBrightDown;
	private Button btnBrightUp;
	private int height = 0;
	
	public static BrightControlView getInstance(Context context) {
		if (instance == null) {
			instance = new BrightControlView(context);
		}
		instance.mContext = context;
		return instance;
	}
	
	private BrightControlView(Context context) {
		mContext = context;
		initView(context);
	}
	
	@SuppressLint ("InlinedApi")
    public void show(View viewParent) {
		popupWindow.setFocusable(true);
		
		final int[] location = new int[2];   
		viewParent.getLocationOnScreen(location);  
		
//		popupWindow.showAtLocation(viewParent, Gravity.NO_GRAVITY, 0, location[1]-100);
		popupWindow.showAtLocation(viewParent, Gravity.START, 0, location[1]);
//		popupWindow.showAsDropDown(viewParent, 0, -(viewParent.getHeight()+height));
	}
	
	
	public void initView(Context context) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.menu_pop_bright, null);
		
		height = view.getHeight();
		sbBright = (SeekBar) view.findViewById(R.id.menu_pop_bright_sb);
		btnBrightDown = (Button) view.findViewById(R.id.menu_pop_bright_left_btn);
		btnBrightUp = (Button) view.findViewById(R.id.menu_pop_bright_right_btn);
		btnBrightDown.setOnClickListener(this);
		btnBrightUp.setOnClickListener(this);
		sbBright.setFocusable(true);
//		sbBright.setProgress(getScreenBrightness());
		sbBright.setProgress(getProgress());
		sbBright.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//				WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
//				lp.screenBrightness = (55 + progress * 2) / 255.0f;
//				((Activity) mContext).getWindow().setAttributes(lp);
				setBright(progress);
//				Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, progress);
			}
		});
		popupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
		popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.color.common_transparent));
		popupWindow.setOutsideTouchable(true);// 设置pWindow之外的布局可点击
		popupWindow.setTouchInterceptor(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
				{
					popupWindow.dismiss();
					return true;
				}
				return false;
			}
		});
	}
	
	public int getScreenBrightness() {
	    int value = 0;
	    try {
	        value = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
	    } catch (SettingNotFoundException e) {
	        e.printStackTrace();
	    }
	    return value;
	}

	@Override
	public void onClick(View v) {
		int progress = sbBright.getProgress();
		switch (v.getId()) {
			case R.id.menu_pop_bright_left_btn:
				progress = progress - 10 <= 0 ? 0 : progress - 10;
				break;
			case R.id.menu_pop_bright_right_btn:
				progress = progress + 10 >= 100 ? 100 : progress + 10;
				break;
		}
		sbBright.setProgress(progress);
	}
	
	private void setBright(int progress) {
		WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
		lp.screenBrightness = (55 + progress * 2) / 255.0f;
		((Activity) mContext).getWindow().setAttributes(lp);
	}
	
	private int getProgress() {
		WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
		int progress = (int) ((lp.screenBrightness * 255 - 55) / 2);
		if (progress < 0) {
			progress = 0;
		}
		return progress;
	}

}
